package com.crc.luojian.beans.model;

import com.crc.luojian.factory.BeanFactory;
import com.crc.luojian.factory.impl.DefaultBeanFactory;
import com.crc.luojian.util.ComponentBeanReader;

import java.applet.AppletContext;

/**
 * @program: luojian-springframework
 * @description:
 * @author: luojian58
 * @create: 2021-06-10 10:45
 */
public class ApplicationContext implements BeanFactory {

    private DefaultBeanFactory factory = new DefaultBeanFactory();

    public ApplicationContext(){

    }
    public void refresh(){
        //1: 将所有的beanDefinition设置到map中
        loadBeanDefinitions(factory);
        //2 设置beanPostProcessor
        loadBeanPostProcessor(factory);
        //3 初始化 beanDefinitionMap 中的bean
        finishBeanFactoryInitialization(factory);
    }

    private void loadBeanPostProcessor(DefaultBeanFactory factory) {
        factory.loadBeanPostProcessor();
    }

    private void finishBeanFactoryInitialization(DefaultBeanFactory factory) {
        factory.preInstantiateSingletons();
    }

    private void loadBeanDefinitions(DefaultBeanFactory factory) {
        ComponentBeanReader componentBeanReader = new ComponentBeanReader();
        componentBeanReader.loadBeanDefinitions(factory);
    }

    @Override
    public Object getBean(String name) {
        return factory.getBean(name);
    }

    @Override
    public <T> T getBean(Class<T> type) {
        return factory.getBean(type);
    }

    @Override
    public boolean containsBean(String name) {
        return factory.containsBean(name);
    }
}
